#!/bin/env ruby
# encoding: utf-8
# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require 'uri'
require 'cgi'

class Unauthorized < Exception; end

class ApplicationController < ActionController::Base
  include Redmine::I18n
  include Redmine::Pagination
  include RoutesHelper
  helper :routes
  
  #added by william
  helper :watchers

  class_attribute :accept_api_auth_actions
  class_attribute :accept_rss_auth_actions
  class_attribute :model_object

  layout 'base'

  protect_from_forgery
  def handle_unverified_request
    super
    cookies.delete(autologin_cookie_name)
  end

  before_filter :user_agent

  rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
  rescue_from ::Unauthorized, :with => :deny_access
  rescue_from ::ActionView::MissingTemplate, :with => :missing_template

  include Redmine::Search::Controller
  include Redmine::MenuManager::MenuController
  helper Redmine::MenuManager::MenuHelper
  
  def user_agent
    logger.info "HTTP_USER_AGENT #{request.env["HTTP_USER_AGENT"]}" 
  end

  def session_expiration
    if session[:user_id]
      if session_expired? && !try_to_autologin
        reset_session
        flash[:error] = l(:error_session_expired)
        redirect_to signin_url
      else
        session[:atime] = Time.now.utc.to_i
      end
    end
  end

     
  def session_expired?
    if Setting.session_lifetime?
      unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60)
        return true
      end
    end
    if Setting.session_timeout?
      unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60)
        return true
      end
    end
    false
  end

  def start_user_session(user)
    session[:user_id] = user.id
    session[:ctime] = Time.now.utc.to_i
    session[:atime] = Time.now.utc.to_i
  end

  # Returns the current user or nil if no user is logged in
  # and starts a session if needed
  def current_user
     find_current_user
  end
  def find_current_user
    user = nil
    unless api_request?
      if session[:user_id]
        # existing session
        user = (User.active.find(session[:user_id]) rescue nil)
      elsif autologin_user = try_to_autologin
        user = autologin_user
      elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
        # RSS key authentication does not start a session
        user = User.find_by_rss_key(params[:key])
      end
    end
    if user.nil? && Setting.rest_api_enabled? && accept_api_auth?
      if (key = api_key_from_request)
        # Use API key
        user = User.find_by_api_key(key)
      else
        # HTTP Basic, either username/password or API key/random
        authenticate_with_http_basic do |username, password|
          user = User.try_to_login(username, password) || User.find_by_api_key(username)
        end
      end
      # Switch user if requested by an admin user
      if user && user.admin? && (username = api_switch_user_from_request)
        su = User.find_by_login(username)
        if su && su.active?
          logger.info("  User switched by: #{user.login} (id=#{user.id})") if logger
          user = su
        else
          render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412
        end
      end
    end
    user
  end

  def autologin_cookie_name
    Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
  end

  def try_to_autologin
    if cookies[autologin_cookie_name] && Setting.autologin?
      # auto-login feature starts a new session
      user = User.try_to_autologin(cookies[autologin_cookie_name])
      if user
        reset_session
        start_user_session(user)
      end
      user
    end
  end

  # Sets the logged in user
  def logged_user=(user)
    reset_session
    if user && user.is_a?(User)
      User.current = user
      start_user_session(user)
    else
      User.current = User.anonymous
    end
  end

  def set_localization
    lang = nil
    if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
      accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
      if !accept_lang.blank?
        accept_lang = accept_lang.downcase
        lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
      end
    end
    lang ||= Setting.default_language
    set_language_if_valid(lang)
  end

  def require_admin
    return unless require_login
    if !User.current.admin?
      render_403
      return false
    end
    true
  end

  # Authorize the user for the requested action
  def authorize(ctrl = params[:controller], action = params[:action], global = false)
    allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
    if allowed
      true
    else
      if @project && @project.archived?
        render_403 :message => :notice_not_authorized_archived_project
      else
        deny_access
      end
    end
  end

  # Authorize the user for the requested action outside a project
  def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
    authorize(ctrl, action, global)
  end

  # Find project of id params[:id]
  def find_project
    @project = Project.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  # Find project of id params[:project_id]
  def find_project_by_project_id
    @project = Project.find(params[:project_id])
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  # Find a project based on params[:project_id]
  # TODO: some subclasses override this, see about merging their logic
  def find_optional_project
    @project = Project.find(params[:project_id]) unless params[:project_id].blank?
    allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
    allowed ? true : deny_access
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  # Finds and sets @project based on @object.project
  def find_project_from_association
    render_404 unless @object.present?

    @project = @object.project
  end

  def find_model_object
    model = self.class.model_object
    if model
      @object = model.find(params[:id])
      self.instance_variable_set('@' + controller_name.singularize, @object) if @object
    end
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  def self.model_object(model)
    self.model_object = model
  end

  # Find the issue whose id is the :id parameter
  # Raises a Unauthorized exception if the issue is not visible
  def find_issue
    # Issue.visible.find(...) can not be used to redirect user to the login form
    # if the issue actually exists but requires authentication
    @issue = Issue.find(params[:id])
    raise Unauthorized unless @issue.visible?
    @project = @issue.project
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  # Find issues with a single :id param or :ids array param
  # Raises a Unauthorized exception if one of the issues is not visible
  def find_issues
    @issues = Issue.find_all_by_id(params[:id] || params[:ids])
    raise ActiveRecord::RecordNotFound if @issues.empty?
    raise Unauthorized unless @issues.all?(&:visible?)
    @projects = @issues.collect(&:project).compact.uniq
    @project = @projects.first if @projects.size == 1
  rescue ActiveRecord::RecordNotFound
    render_404
  end

  # make sure that the user is a member of the project (or admin) if project is private
  # used as a before_filter for actions that do not require any particular permission on the project
  def check_project_privacy
    if @project && !@project.archived?
      if @project.visible?
        true
      else
        deny_access
      end
    else
      @project = nil
      render_404
      false
    end
  end

  def back_url
    url = params[:back_url]
    if url.nil? && referer = request.env['HTTP_REFERER']
      url = CGI.unescape(referer.to_s)
    end
    url
  end

  def redirect_back_or_default(default, options={})
    back_url = params[:back_url].to_s
    if back_url.present?
      begin
        uri = URI.parse(back_url)
        # do not redirect user to another host or to the login or register page
        if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
          redirect_to(back_url)
          return
        end
      rescue URI::InvalidURIError
        logger.warn("Could not redirect to invalid URL #{back_url}")
        # redirect to default
      end
    elsif options[:referer]
      redirect_to_referer_or default
      return
    end
    redirect_to default
    false
  end

  # Redirects to the request referer if present, redirects to args or call block otherwise.
  def redirect_to_referer_or(*args, &block)
    redirect_to :back
  rescue ::ActionController::RedirectBackError
    if args.any?
      redirect_to *args
    elsif block_given?
      block.call
    else
      raise "#redirect_to_referer_or takes arguments or a block"
    end
  end

  def render_403(options={})
    @project = nil
    render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
    return false
  end

  def render_404(options={})
    render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
    return false
  end

  # Renders an error response
  def render_error(arg)
    arg = {:message => arg} unless arg.is_a?(Hash)

    @message = arg[:message]
    @message = l(@message) if @message.is_a?(Symbol)
    @status = arg[:status] || 500

    respond_to do |format|
      format.html {
        render :template => 'common/error', :layout => use_layout, :status => @status
      }
      format.any { head @status }
    end
  end

  # Handler for ActionView::MissingTemplate exception
  def missing_template
    logger.warn "Missing template, responding with 404"
    @project = nil
    render_404
  end

  # Picks which layout to use based on the request
  #
  # @return [boolean, string] name of the layout to use or false for no layout
  def use_layout
     request.xhr? ? false : 'base'
  end

  def invalid_authenticity_token
    if api_request?
      logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)."
    end
    render_error "Invalid form authenticity token."
  end

  def render_feed(items, options={})
    @items = items || []
    @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
    @items = @items.slice(0, Setting.feeds_limit.to_i)
    @title = options[:title] || Setting.app_title
    render :template => "common/feed", :formats => [:atom], :layout => false,
           :content_type => 'application/atom+xml'
  end

  def self.accept_rss_auth(*actions)
    if actions.any?
      self.accept_rss_auth_actions = actions
    else
      self.accept_rss_auth_actions || []
    end
  end

  def accept_rss_auth?(action=action_name)
    self.class.accept_rss_auth.include?(action.to_sym)
  end

  def self.accept_api_auth(*actions)
    if actions.any?
      self.accept_api_auth_actions = actions
    else
      self.accept_api_auth_actions || []
    end
  end

  def accept_api_auth?(action=action_name)
    self.class.accept_api_auth.include?(action.to_sym)
  end

  # Returns the number of objects that should be displayed
  # on the paginated list
  def per_page_option
    per_page = nil
    if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
      per_page = params[:per_page].to_s.to_i
      session[:per_page] = per_page
    elsif session[:per_page]
      per_page = session[:per_page]
    else
      per_page = Setting.per_page_options_array.first || 25
    end
    per_page
  end

  # Returns offset and limit used to retrieve objects
  # for an API response based on offset, limit and page parameters
  def api_offset_and_limit(options=params)
    if options[:offset].present?
      offset = options[:offset].to_i
      if offset < 0
        offset = 0
      end
    end
    limit = options[:limit].to_i
    if limit < 1
      limit = 25
    elsif limit > 100
      limit = 100
    end
    if offset.nil? && options[:page].present?
      offset = (options[:page].to_i - 1) * limit
      offset = 0 if offset < 0 
      offset ||= 0
    end
   

    [offset, limit]
  end

  # qvalues http header parser
  # code taken from webrick
  def parse_qvalues(value)
    tmp = []
    if value
      parts = value.split(/,\s*/)
      parts.each {|part|
        if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
          val = m[1]
          q = (m[2] or 1).to_f
          tmp.push([val, q])
        end
      }
      tmp = tmp.sort_by{|val, q| -q}
      tmp.collect!{|val, q| val}
    end
    return tmp
  rescue
    nil
  end

  # Returns a string that can be used as filename value in Content-Disposition header
  def filename_for_content_disposition(name)
    request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
  end

  def api_request?
    %w(xml json).include? params[:format]
  end

  # Returns the API key present in the request
  def api_key_from_request
    if params[:key].present?
      params[:key].to_s
    elsif request.headers["X-Redmine-API-Key"].present?
      request.headers["X-Redmine-API-Key"].to_s
    end
  end

  # Returns the API 'switch user' value if present
  def api_switch_user_from_request
    request.headers["X-Redmine-Switch-User"].to_s.presence
  end

  # Sets the `flash` notice or error based the number of issues that did not save
  #
  # @param [Array, Issue] issues all of the saved and unsaved Issues
  # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved
  def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
    if unsaved_issue_ids.empty?
      flash[:notice] = l(:notice_successful_update) unless issues.empty?
    else
      flash[:error] = l(:notice_failed_to_save_issues, 
                        :count => unsaved_issue_ids.size,
                        :total => issues.size,
                        :ids => '#' + unsaved_issue_ids.join(', #'))
    end
  end

  # Rescues an invalid query statement. Just in case...
  def query_statement_invalid(exception)
    logger.error "Query::StatementInvalid: #{exception.message}" if logger
    session.delete(:query)
    sort_clear if respond_to?(:sort_clear)
    render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  end

  # Renders a 200 response for successfull updates or deletions via the API
  def render_api_ok
    render_api_head :ok
  end

  # Renders a head API response
  def render_api_head(status)
    # #head would return a response body with one space
    render :text => '', :status => status, :layout => nil
  end

  # Renders API response on validation failure
  def render_validation_errors(objects)
    if objects.is_a?(Array)
      @error_messages = objects.map {|object| object.errors.full_messages}.flatten
    else
      @error_messages = objects.errors.full_messages
    end
    render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil
  end

  # Overrides #_include_layout? so that #render with no arguments
  # doesn't use the layout for api requests
  def _include_layout?(*args)
    api_request? ? false : super
  end

  ## Please added code in html.
  # <div class="pagination"><%= pagination_links_full @obj_pages, @obj_count, :per_page_links => false %></div>
  def paginateHelper obj, pre_size=20
    @obj_count = obj.count
    @obj_pages = Paginator.new @obj_count, pre_size, params['page']
    if obj.kind_of? ActiveRecord::Base or obj.kind_of?  ActiveRecord::Relation
      obj.limit(@obj_pages.per_page).offset(@obj_pages.offset)
    elsif obj.kind_of? Array
      obj[@obj_pages.offset, @obj_pages.per_page]
    else
      logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
      raise RuntimeError, 'unknow type, Please input you type into this helper.'
    end
  end
  
=begin
  #用于添加用户行为的日志文件
  def append_info_to_payload(payload)
    super
    
    type = nil #最先初始化为nil 表示不插入日志（没有匹配到操作）
    
    #创建一个新的日志
    filename = File.join(Rails.root, 'log', 'user_trace_search.log')
    logger_search = Logger.new(filename)
    
    #创建用户click的日志
    filename = File.join(Rails.root, 'log', 'user_trace_click.log')
    logger_click = Logger.new(filename)
    
    session_id = request.session_options[:id] #获取用户的session_id
    ip = request.remote_ip #获取访问用户的ip地址
    host = request.host
    #根据controller和action判断用户执行的是什么操作
    controller = params[:controller]
    action = params[:action]
    #获取时间
    time = Time.new
    year = time.year.to_s
    month = time.month.to_s
    day = time.day.to_s
    hour = time.hour.to_s
    minute = time.min.to_s
    second = time.sec.to_s
    time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    
    #根据controller和action判断用户进行了什么操作
    if controller == "open_source_projects" && action == 'search'
      #表示用户执行了项目的查询操作
      type = "search_project"
      name = params[:name]
      parameter = Hash["name" => name]
      #向session中插入查询的关键词 便于后面看是否是用户查询后点击的项目
      session[:keyword_project] = name
      
      #获取对应的页码和当前页码中项目id
      ids = []
      if @projects_pages != nil
        #表示采用了分页
        page = @projects_pages.page
        session[:page_project_list] = page
        projects = @open_source_projects
        projects.each do |project|
          ids << project.id
        end
        session[:current_page_project_ids] = ids #记录当前分页的所有项目id
      end
    end
    
    if controller == "open_source_projects" && action == "index"
      #表示用户点击了项目列表页 或点击了分页页码
      #判断用户是点击了项目列表页按钮还是点击了分页按钮
      url = request.url
      if url.index('page=') != nil
        #表示点击了分页
        type = nil #不用记录这个操作
      else
        type = "click_project_list_button" #表示点击了项目列表按钮
        #清除session中的keyword
        session[:keyword_project] = nil
        session[:keytag_project] = nil
      end
      
      parameter = Hash.new
      #获取对应的页码和当前页码中项目id
      ids = []
      if @projects_pages != nil
        #表示采用了分页
        page = @projects_pages.page
        session[:page_project_list] = page
        projects = @open_source_projects
        projects.each do |project|
          ids << project.id
        end
        session[:current_page_project_ids] = ids #记录当前分页的所有项目id
      end
    end
    
    if controller == "open_source_projects" && action == "show"
      #可能的情况 项目列表页中的项目名、查询结果列表中的项目名;直接输入URL地址和云标签点击包括在前面两种情况中 相似软件 相关软件这两种情况也被归结到了前两种 按标签查询后点击项目名
      id = params[:id]
      keyword = session[:keyword_project]
      if keyword != nil
        #表示用户search_project后的项目
        type = "click_project_with_keyword"
        page = session[:page_project_list]
        #查看是哪个id
        ids = session[:current_page_project_ids]
        for i in (0..ids.length-1)
          if id.to_i == ids[i].to_i
            rank = i + 1
            break
          end
        end
        if rank == nil
          #表示是点击的云标签
          type = "click_project_without_keyword"
          parameter = Hash["osp_id" => id]
        else
          #表示正常情况
          parameter = Hash["keyword" => keyword, "page" => page, "rank" => rank, "osp_id" => id]
        end
        
      else
        #表示是用户点击列表页后显示的 或tag查询
        keytags = session[:keytag_project]
        if keytags != nil
          #表示是根据tag进行查询后点击的
          type = "click_project_after_search_tags"
          page = session[:page_project_list]
          if page == nil
            type = nil #正常情况下没有这种情况
          else
            ids = session[:current_page_project_ids]
            for i in (0..ids.length-1)
              if id.to_i == ids[i].to_i
                rank = i + 1
                break
              end
            end
            if rank == nil
              type = nil
            else
              parameter = Hash["keytags" => keytags, "page" => page, "rank" => rank, "osp_id" => id]
            end
          end
        else
          type = "click_project_without_keyword"
          page = session[:page_project_list]
          if page == nil
            #表示是云标签或输入url的情况
            parameter = Hash["osp_id" => id]
          else
            #表示是点击列表页按钮后的点击 因此还保留着分页
            ids = session[:current_page_project_ids]
            for i in (0..ids.length-1)
              if id.to_i == ids[i].to_i
                rank = i + 1
                break
              end
            end
            if rank == nil
              parameter = Hash["osp_id" => id]
            else
              parameter = Hash["page" => page, "rank" => rank, "osp_id" => id]
            end
          end
        end
        
      end
    end
    
    if controller == "relative_memos" && action == "search"
      #表示用户搜索框查询帖子操作
      type = "search_knowledge"
      sort = @sort_tag #搜索结果按什么排序 ""-表示按关联度下降排序
      if sort == ""
        sort = "relation DESC"
      end
      content = params[:name]
      parameter = Hash["content" => content, "sort_by" => sort]
      session[:keyword_knowledge] = content
      
      ids = []
      if @topics_pages != nil
        #表示采用了分页
        page = @topics_pages.page
        session[:page_knowledge_list] = page
        topics = @topics
        topics.each do |topic|
          ids << topic.id
        end
        session[:current_page_knowledge_ids] = ids #记录当前分页的所有项目id
      end
    end
    
    if controller == "" && action == ""
      #表示用户点击了帖子标题
      type = nil #由于是链接到外站 因此需要在页面处用ajax处理
    end
    
    if controller == "" && action == ""
      #表示用户点击项目详情页面的来源网站
      # type = "click_source_website"
      type = nil
    end
    
    if controller == "tags" && action == "index"
      #表示用户点击项目标签或增加、删除标签
      type = "search_project_by_tag"
      selected_tags = @selected_tags
      osp_id = @obj_id
      tags = ""
      selected_tags.each do |t|
        tags = tags + "<" + t.to_s + ">,"
      end
      if tags.length > 0
        tags = tags[0,tags.length-1]
      end
      parameter = Hash["tags" => tags.strip, "osp_id" => osp_id]
      
      session[:keyword_project] = nil
      session[:keytag_project] = tags
      
      ids = []
      page = @obj_pages.page
      session[:page_project_list] = page
      projects = @open_source_projects_results
      projects.each do |project|
        ids << project.id
      end
      session[:current_page_project_ids] = ids #记录当前分页的所有项目id
    end
    
    if controller == "open_source_projects" && action == "remove_condition"
      #表示移除查询条件 这个操作是在查询的基础上 后面实现
      # type = "click_remove_condition"
      
    end
    
    #将type、session_id、ip、host、time以及parameter存储在相应的文件中
    if type != nil
      #表示type不是空的 即上面至少有一种情况比配到
      str = "type:" + type + "\t" + "session_id:" + session_id + "\t" + "ip:" + ip + "\t" + "host:" + host + "\t" + "time:" + time + "\t"
      keys_in_parameter = parameter.keys
      keys_in_parameter.each do |k|
        value = parameter[k]
        str = str + "[" + k + "=>" + value.to_s + "]"
      end
      if type == "search_project" || type == "search_knowledge" || type == "search_project_by_tag"
        logger_search.info(str.strip)
      else
        #表示是各种点击操作
        logger_click.info(str.strip)
      end
    end
  end
=end
end
